package com.android.myipc.provide;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.android.myipc.Book;
import com.android.myipc.R;
import com.android.myipc.User;

/**
 * Created by Android Studio.
 * User: pengym
 * Date: 2020/11/11
 * Time: 17:36
 * Name: 作用类：
 */
public class ProvideActivity extends AppCompatActivity {
    private static final String TAG = "ProvideActivity";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Uri uri = Uri.parse("content://com.android.myipc.provider/book");
        getContentResolver().query(uri, null, null, null, null);
        getContentResolver().query(uri, null, null, null, null);
        getContentResolver().query(uri, null, null, null, null);
        ContentValues values = new ContentValues();
        values.put("_id", 6);
        values.put("name", "程序设计的艺术");
        getContentResolver().insert(uri, values);
        Cursor bookCursor = getContentResolver().query(uri, new String[]{"_id", "name"}, null, null, null);
        while (bookCursor.moveToNext()) {
            Book book = new Book();
            book.bookId = bookCursor.getInt(0);
            book.BookName = bookCursor.getString(1);
            Log.d(TAG, "query book :" + book.toString());
        }
        bookCursor.close();
        Uri userUri = Uri.parse("content://com.android.myipc.provider/user");
        Cursor userCursor = getContentResolver().query(userUri, new String[]{"_id", "name", "sex"}, null, null, null);
        while (userCursor.moveToNext()) {
            User user = new User();
            user.userName = userCursor.getString(1);
            user.userId = userCursor.getInt(0);
            user.isMale = userCursor.getInt(2) == 1;
            Log.d(TAG, "query user:" + user.toString());
        }
        userCursor.close();

    }

/**
 * 知识点：Cursor
 *  Cursor是一个数据库操作类，主要用户读取数据库中数据
 *  Cursor一共有一下方法
 *  c.move(int offset); //以当前位置为参考,移动到指定行
 *  c.moveToFirst();    //移动到第一行
 *  c.moveToLast();     //移动到最后一行
 *  c.moveToPosition(int position); //移动到指定行
 *  c.moveToPrevious(); //移动到前一行
 *  c.moveToNext();     //移动到下一行
 *  c.isFirst();        //是否指向第一条
 *  c.isLast();     //是否指向最后一条
 *  c.isBeforeFirst();  //是否指向第一条之前
 *  c.isAfterLast();    //是否指向最后一条之后
 *  c.isNull(int columnIndex);  //指定列是否为空(列基数为0)
 *  c.isClosed();       //游标是否已关闭
 *  c.getCount();       //总数据项数
 *  c.getPosition();    //返回当前游标所指向的行数
 *  c.getColumnIndex(String columnName);//返回某列名对应的列索引值，如果不存在返回-1
 *  c.getString(int columnIndex);   //返回当前行指定列的值
 *  c·getColumnIndexOrThrow(String columnName)——从零开始返回指定列名称，如果不存在将抛出IllegalArgumentException 异常。
 *  c.close()——关闭游标，释放资源
 */
}

